package com.cy.zs.hp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.zs.hp.common.exception.HpException;
import com.cy.zs.hp.feign.service.IHisDoctorFeignService;
import com.cy.zs.hp.feign.service.IHisRegisterFeignService;
import com.cy.zs.hp.mapper.HisUserMapper;
import com.cy.zs.hp.model.HisPatient;
import com.cy.zs.hp.model.HisRegister;
import com.cy.zs.hp.model.HisUser;
import com.cy.zs.hp.model.SchedulingTable;
import com.cy.zs.hp.service.IHisUserService;
import com.cy.zs.web.util.JsonResult;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 该表保存注册用户信息 服务实现类
 * </p>
 *
 * @author cgb2008
 * @since 2020-11-28
 */
@Service
public class HisUserServiceImpl extends ServiceImpl<HisUserMapper, HisUser> implements IHisUserService {
   @Autowired
   HisUserMapper mapper;
   @Autowired
   private IHisDoctorFeignService iHisDoctorFeignService;
   @Autowired
   private IHisRegisterFeignService iHisRegisterFeignService;

    @Override
    public int insert(HisUser hisUser) {
        String phone=hisUser.getPhone();
        String result=mapper.findByPhone(phone);
        System.out.println("result="+result);
        if (result!=null)throw new  HpException("您的电话号码已被占用,请重新注册");
        String salt= UUID.randomUUID().toString();
        SimpleHash sh=new SimpleHash("md5",hisUser.getPassword(),salt,1);
        String hashPwd=sh.toHex();
        hisUser.setPassword(hashPwd);
        hisUser.setSalt(salt);
        hisUser.setUserName(phone);
        //hisUser.setId(0);
        int rows=mapper.insert(hisUser);
        if (rows!=1)throw  new  HpException("注册失败！插入数据时出现错误，请稍后再次尝试！");
        return rows;
    }

    @Override
    public int login(HisUser hisUser) {
        String phone=hisUser.getPhone();
        String result=mapper.findByPhone(phone);
       String password=mapper.findByPassword(phone);
        String salt=mapper.findBySalt(phone);

        if (result==null)throw new HpException("登录失败，该用户不存在");
        SimpleHash simpleHash=new SimpleHash("md5",hisUser.getPassword(),salt,1);
        String hashPwd=simpleHash.toHex();
        if (!hashPwd.equals(password)) throw  new  HpException("登录失败，密码不正确");
        return 0;
    }

    /**
     * 远程调用 查询医生工作表
     * @param id
     * @return
     */
    @Override
    public List<SchedulingTable> findWorkTableByDoctorId(Integer id) {
       JsonResult< List<SchedulingTable>> jsonResult = iHisDoctorFeignService.findTimesByDoctorId(id);
        List<SchedulingTable> list = jsonResult.getData();
        return list;
    }

    @Override
    public List<HisPatient> addPatient(HisPatient hisPatient) {
        JsonResult<List<HisPatient>> jsonResult = iHisRegisterFeignService.doAddPatient(hisPatient);
        List<HisPatient> list = jsonResult.getData();
        return list;

    }

    @Override
    public Integer addRegister(HisRegister hisRegister) {
        JsonResult result = iHisRegisterFeignService.doAddRegister(hisRegister);
        String msg = result.getMsg();
        System.out.println("返回结果类型"+msg);
        return 1;
    }


}
